package com.zjp.dataFormat.service.rangeTime;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zjp.dataFormat.pojo.common.CommonTimeQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class RangeTimeService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void handleTimeRange(CommonTimeQuery query) {
        Integer rangeState = query.getRangeState();
        if (ObjectUtil.isNotEmpty(rangeState)) {
            DateTime date = DateUtil.date();
            switch (rangeState) {
                case 1:
                    Integer rangeType = query.getRangeType();
                    if (ObjectUtil.isNotEmpty(rangeType)) {
                        rangeTime(query, rangeType, date);
                    }
                    break;
                case 2:
                    Integer offsetNum = query.getOffsetNum();
                    if (ObjectUtil.isNotEmpty(offsetNum)) {
                        offsetTime(query, offsetNum, date);
                    }
                    break;
                default:
                    throw new RuntimeException("rangeState 参数 错误");
            }
        }
    }

    public void rangeTime(CommonTimeQuery dto, Integer rangeType, DateTime date) {
        switch (rangeType) {
            //
            case 0:
                break;
            // 昨日
            case 1:
                DateTime yesterday = DateUtil.yesterday();
                String beginOfDay = DateUtil.formatDate(DateUtil.beginOfDay(yesterday));
                String endOfDay = DateUtil.formatDate(DateUtil.endOfDay(yesterday));
                dto.setBeginTime(beginOfDay);
                dto.setEndTime(endOfDay);
                break;
            case 2:
                // 本周
                String beginOfWeek = DateUtil.formatDate(DateUtil.beginOfWeek(date));
                String endOfWeek = DateUtil.formatDate(DateUtil.endOfWeek(date));
                dto.setBeginTime(beginOfWeek);
                dto.setEndTime(endOfWeek);
                break;
            case 3:
                // 本月
                String beginOfMonth = DateUtil.formatDate(DateUtil.beginOfMonth(date));
                String endOfMonth = DateUtil.formatDate(DateUtil.endOfMonth(date));
                dto.setBeginTime(beginOfMonth);
                dto.setEndTime(endOfMonth);
                break;
            case 4:
                if (logger.isDebugEnabled()) {
                    logger.debug("自定义查询");
                }
                break;
            default:
                throw new RuntimeException("Unexpected rangeType value: " + rangeType);
        }
        String beginTime = dto.getBeginTime();
        String endTime = dto.getEndTime();
        if (logger.isDebugEnabled()) {
            logger.debug("==handleRangeTime==rangeType=={}", rangeType);
            logger.debug("==handleRangeTime==beginTime=={}", beginTime);
            logger.debug("==handleRangeTime==endTime=={}", endTime);
        }
    }

    public void offsetTime(CommonTimeQuery dto, Integer num, DateTime date) {
        logger.warn("==开始处理时间偏移=={}", num);
        if (num > 0) {
            String beginOfDay = DateUtil.formatDate(DateUtil.beginOfDay(date));
            String endOfDay = DateUtil.formatDate(DateUtil.endOfDay(DateUtil.offsetDay(date, num)));
            dto.setBeginTime(beginOfDay);
            dto.setEndTime(endOfDay);
        } else if (num < 0) {
            String beginOfDay = DateUtil.formatDate(DateUtil.beginOfDay(DateUtil.offsetDay(date, num)));
            String endOfDay = DateUtil.formatDate(DateUtil.endOfDay(date));
            dto.setBeginTime(beginOfDay);
            dto.setEndTime(endOfDay);
        } else {
            String beginOfDay = DateUtil.formatDate(DateUtil.beginOfDay(date));
            String endOfDay = DateUtil.formatDate(DateUtil.endOfDay(date));
            dto.setBeginTime(beginOfDay);
            dto.setEndTime(endOfDay);
        }
    }


    public void clearData(CommonTimeQuery dto) {
        if (logger.isDebugEnabled()) {
            logger.debug("==clearData==");
        }
        dto.setBeginTime(null);
        dto.setEndTime(null);
        dto.setRangeType(null);
        dto.setRangeState(null);
        dto.setOffsetNum(null);
    }

    public void log(CommonTimeQuery query) {
        logger.info("==log=={}",query);
    }

}
